<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\cms\api\controller;

use think\facade\Filesystem;
use think\exception\ValidateException;
use app\api\BaseController;
use plugins\cms\api\validate\Job as Validate;
use plugins\cms\api\model\Job as JobModel;
use app\api\model\User;
use plugins\cms\api\model\JobResume;
/**
 * 招聘
 */
class Job extends BaseController
{
    /**
     * 列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('index')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $order = ! empty($input['prop']) && ! empty($input['order']) ? [$input['prop'] => $input['order']] : ['istop' => 'desc', 'sort' => 'desc', 'create_time' => 'desc'];
            $append = ['url','catalog_title','resume_count'];
            $search = ['keyword', 'date', 'attr', 'catalog_id', 'status'];
            $data   = JobModel::withSearch($search, $input)->order($order)->append($append)->page($input['page'],10)->select();
            $count  = JobModel::withSearch($search, $input)->count();
            return json(['status' => 'success','message' => '获取成功', 'data' => $data, 'count' => $count]);
        }
    }

    /**
     * 详情
     */
    public function single()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('single')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $where[] = ['id', '=', $input['id']];
            $append  = ['catalog_title','resume_count','relation_list'];
            $data = JobModel::where($where)->append($append)->find();
            if (! $data) {
                return json(['status'=>'error', 'message'=> '招聘信息不存在']);
            }
            // 上下条
            if (! empty($input['catalog_id'])) {
                $field = 'id,catalog_id,title,seo_url';
                $npWhere[] = ['catalog_id', 'find in set', $input['catalog_id']];
                $npWhere[] = ['status', '=', 1];
                $data->prev = JobModel::where('id','>',$data->id)->field($field)->where($npWhere)->order('id', 'asc')->find();
                $data->next = JobModel::where('id','<',$data->id)->field($field)->where($npWhere)->order('id', 'desc')->find();
            }
            return json(['status' => 'success','message' => '获取成功', 'data' => $data]);
        }
    }
    
    /**
     * 投递简历
     */
    public function resume()
    {
        if ($this->request->isPost()) {
            $where[] = ['status', '=', 1];
            $where[] = ['id', '=', input('post.id')];
            $data = JobModel::where($where)->find();
            // 不存在404
            if (!$data) {
                return json(['status' => 'error','message' => '招聘工作不存在']); 
            }
            // 权限
            if (! empty($data->group_id)) {
                if (empty($this->request->userInfo)) {
                    return json(['status' => 'error','message' => '简历投递权限不足']);
                }
                if (! in_array($this->request->userInfo->group_id, $data->group_id)) {
                    return json(['status' => 'error','message' => '简历投递权限不足']);
                }
            }
            try {
                $file = $this->request->file('file');
                validate([ 'file' => ['fileExt' => 'docx,doc', 'fileSize' => 5*1024*1024]])->check(['file' => $file]);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $url  = Filesystem::putFile('user', $file);
            $word = '/upload/' . str_replace('\\', '/', $url);
            // 投递记录
            JobResume::create([
                'job_id'       => $data->id,
                'job_title'    => $data->title,
                'job_cover'    => $data->cover,
                'user_id'      => $data->request->userInfo ? $this->request->userInfo->id : 0,
                'word'         => $word,
                'create_time'  => date('Y-m-d H:i:s'),
            ]);
            return json(['status' => 'success','message' => '提交成功']);
        }
    }
}